package com.ming.sportdemo.config;

import com.ming.sportdemo.filter.Authentication;
import com.ming.sportdemo.filter.RoleAuthentication;
import com.ming.sportdemo.filter.UtilAuthentication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//全局配置类--配置跨域请求
@Configuration
public class WebConfig implements WebMvcConfigurer{
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /**
         * 1.预访问的路劲
         * 2.请求来源
         * 3.方法
         * 4.允许携带
         * 5.响应最大时间
         */
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","POST","PUT","OPTIONS","DELETE")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
/*        registry.addInterceptor(authentication())
                .addPathPatterns("/**");
        registry.addInterceptor(roleAuthentication())
                .addPathPatterns("/**");*/
//        registry.addInterceptor(utilAuthentication()).addPathPatterns("/**");
    }

    @Bean
    public Authentication authentication() {
        return new Authentication();
    }

    @Bean
    public RoleAuthentication roleAuthentication(){
        return new RoleAuthentication();
    }

    @Bean
    public UtilAuthentication utilAuthentication(){
        return new UtilAuthentication();
    }

}
